package com.haofengsoft.scce.ui.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.haofengsoft.platform.companent.imageview.imageindicator.AutoPlayManager;
import com.haofengsoft.platform.companent.imageview.imageindicator.ImageIndicatorView;
import com.haofengsoft.platform.companent.imageview.imageindicator.network.NetworkImageIndicatorView;
import com.haofengsoft.platform.companent.listview.SpreadListView;
import com.haofengsoft.scce.R;
import com.haofengsoft.scce.adapter.advert.AdvertModel;
import com.haofengsoft.scce.adapter.index.TopNewAdapter;
import com.haofengsoft.scce.adapter.index.TopNewModel;
import com.haofengsoft.scce.common.client.VolleyBean;
import com.haofengsoft.scce.common.client.VolleyInterface;
import com.haofengsoft.scce.common.client.VolleyManage;
import com.haofengsoft.scce.common.config.ConstantConfig;
import com.haofengsoft.scce.controller.AdvertController;
import com.haofengsoft.scce.controller.TopNewController;
import com.haofengsoft.scce.event.MainEvent;

import de.greenrobot.event.EventBus;

/**
 * 首页
 * 
 * @author zhj
 */
public class HomeFragment extends BaseFragment {

	private NetworkImageIndicatorView autoIndicatorIv;
	private AutoPlayManager autoBrocastManager;
	private SpreadListView topNewPtrLv;
	private TopNewAdapter topNewAdapter = null;
	private Context context;

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.main_home, container, false);
		context = getActivity();
		view.setOnTouchListener(new DontSpillOnTouchListener());
		autoIndicatorIv = (NetworkImageIndicatorView) view.findViewById(R.id.main_home_indicatoriv_niviv);
		autoIndicatorIv.setIndicateStyle(ImageIndicatorView.INDICATE_USERGUIDE_STYLE);
		topNewPtrLv = (SpreadListView) view.findViewById(R.id.main_home_top_new_ptrl);
		autoIndicatorIv.setOnItemChangeListener(new ImageIndicatorView.OnItemChangeListener() {
			@Override
			public void onPosition(int position, int totalCount) {

			}
		});
		autoIndicatorIv.setOnItemChangeListener(new ImageIndicatorView.OnItemChangeListener() {
			@Override
			public void onPosition(int position, int totalCount) {

			}
		});
		initAdView();
		Button consultationBtn = (Button) view.findViewById(R.id.index_consultation_btn);
		consultationBtn.setOnClickListener(new MenuOnClickListener(MainEvent.CONSULTATION));
		Button quotationBtn = (Button) view.findViewById(R.id.index_quotation_btn);
		quotationBtn.setOnClickListener(new MenuOnClickListener(MainEvent.QUOTATION));

		Button tradingBtn = (Button) view.findViewById(R.id.index_trading_btn);
		tradingBtn.setOnClickListener(new MenuOnClickListener(MainEvent.TRADING));

		Button optionalBtn = (Button) view.findViewById(R.id.index_optional_btn);
		optionalBtn.setOnClickListener(new MenuOnClickListener(MainEvent.OPTIONAL));
		initTopNew();
		return view;
	}

	private void refreshList(List<TopNewModel> modelList) {
		if (null == topNewAdapter) {
			topNewAdapter = new TopNewAdapter(context, modelList);
			topNewPtrLv.setAdapter(topNewAdapter);
		} else {
			topNewAdapter.addFocusNewList(modelList);
			topNewAdapter.notifyDataSetChanged();
		}
	}

	@Override
	protected void lazyLoad() {

	}

	class MenuOnClickListener implements View.OnClickListener {
		int operType;

		public MenuOnClickListener(int operType) {
			this.operType = operType;
		}

		@Override
		public void onClick(View view) {
			EventBus.getDefault().post(new MainEvent(operType));
		}
	}

	private void initTopNew() {
		Map<String, Object> mapParam = new HashMap<String, Object>();
		mapParam.put("PageIndex", 0);
		mapParam.put("PageSize", 10);
		VolleyManage.getInstance().methodPost(context, null, ConstantConfig.TOP_NEW_URL, mapParam, new TopNewReal());
	}

	class TopNewReal implements VolleyInterface {

		@Override
		public void gainData(VolleyBean bean) {
			if (bean.isSuccess()) {
				List<TopNewModel> modelList = TopNewController.getInstance(getActivity()).gainTopNewModelList(
						bean.getContent());
				refreshList(modelList);
			}
		}

	}

	private void initAdView() {
		List<AdvertModel> infoList = AdvertController.getInstance(getActivity()).gainAdvertList();
		final List<String> urlList = new ArrayList<String>();
		for (AdvertModel model : infoList) {
			urlList.add(ConstantConfig.API_URL + model.getImageUr());
		}
		autoIndicatorIv.setupLayoutByImageUrl(urlList);
		autoIndicatorIv.show();
		autoBrocastManager = new AutoPlayManager(autoIndicatorIv);
		autoBrocastManager.setBroadcastEnable(true);
		autoBrocastManager.setBroadCastTimes(-1);// loop times
		autoBrocastManager.setBroadcastTimeIntevel(3 * 1000, 3 * 1000);// set
	}

	@Override
	public void onResume() {
		super.onResume();
		autoBrocastManager.loop();
	}
}
